package com.framework.boc.mojo;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.util.ArrayList;
import java.util.List;

@Mojo(name = "code-generator")
public class CodeGeneratorMojo extends AbstractMojo {

    @Parameter(property = "dbUrl")
    private String dbUrl;

    @Parameter(property = "driverName")
    private String driverName;

    @Parameter(property = "username")
    private String username;

    @Parameter(property = "password")
    private String password;

    @Parameter(property = "modelName")
    private String modelName;

    @Parameter(property = "packagePath")
    private String packagePath;

    @Parameter(property = "tableNames")
    private String[] tableNames;

    @Parameter(property = "tablePrefix")
    private String tablePrefix;

    private String projectPath = System.getProperty("user.dir");

    public void execute() throws MojoExecutionException, MojoFailureException {

//        System.out.println(dbUrl);
//        System.out.println(driverName);
//        System.out.println(username);
//        System.out.println(password);
//        System.out.println(modelName);
//        System.out.println(tableNames);
//        System.out.println(tablePrefix);
//        System.out.println(projectPath);

        AutoGenerator mpGenerator = new AutoGenerator();
        mpGenerator.setGlobalConfig(getGlobalConfig());
        mpGenerator.setDataSource(getDataSourceConfig());
        mpGenerator.setPackageInfo(getPackageConfig());
        mpGenerator.setCfg(getInjectionConfig());
        mpGenerator.setTemplate(getTemplateConfig());
        mpGenerator.setStrategy(getStrategyConfig());
        mpGenerator.execute();
        System.out.println("execute success........");
    }


    /**
     * 获取全局配置
     * @return
     */
    private GlobalConfig getGlobalConfig() {
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("Leil");
        gc.setOpen(false);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        return gc;
    }


    /**
     * 配置数据源
     * @return
     */
    private DataSourceConfig getDataSourceConfig() {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(dbUrl);
        // dsc.setSchemaName("public");
        dsc.setDriverName(driverName);
        dsc.setUsername(username);
        dsc.setPassword(password);
        return dsc;
    }


    /**
     * 配置包路径
     * @return
     */
    private PackageConfig getPackageConfig() {
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(modelName);
        pc.setParent(packagePath);
        return pc;
    }


    /**
     * 自定义配置
     */
    private InjectionConfig getInjectionConfig() {
        InjectionConfig cfg = new InjectionConfig() {
            public void initMap() {

            }
        };
        // 如果模板引擎是 velocity
        String templatePath = "/templates/mapper.xml.vm";
        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名,如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return projectPath + "/src/main/resources/mapper/"
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        return cfg;
    }



    /**
     * 配置模板
     */
    private TemplateConfig getTemplateConfig() {
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        return templateConfig;
    }


    /**
     * 配置策略
     * @return
     */
    private StrategyConfig getStrategyConfig() {
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(false);
        strategy.setRestControllerStyle(true);
        // 公共父类
        strategy.setSuperControllerClass("com.framework.boc.controller.BaseController");

        strategy.setInclude(tableNames);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(tablePrefix);
        return strategy;
    }


}
